﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Input;
using System.Data;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Messaging;
using GalaSoft.MvvmLight.Command;
using GoldDigger.AppServices;
using GoldDigger.Models;

namespace GoldDigger.ViewModels
{
    public class AssociationRuleViewModel : ViewModelBase
    {
        private MainService _app;
        private double _confValue;
        private double _supportValue;
        private ICommand _runAprioriCommand;
        private ICommand _returnToSummaryCommand;
        private delegate void _aprioriParameters(double param1, double param2, Action<bool> param3);

        public AssociationRuleViewModel(MainService ms)
        {
            _app = ms;
            _confValue = 0.6;
            _supportValue = 0.4;
        }

        public double ConfValue
        {
            get
            {
                return _confValue;
            }
            set
            {
                if (value.GetType() == typeof(double))
                {
                    _confValue = value;
                    RaisePropertyChanged("ConfValue");
                }
            }
        }

        public double SupportValue
        {
            get
            {
                return _supportValue;
            }
            set
            {
                if (value.GetType() == typeof(double))
                {
                    _supportValue = value;
                    RaisePropertyChanged("SupportValue");
                }
            }
        }

        public ICommand RunAprioriCommand
        {
            get
            {
                return _runAprioriCommand ?? (_runAprioriCommand = new RelayCommand(GenerateAssociationRules));
            }
        }

        public ICommand ReturnToSummaryCommand
        {
            get
            {
                return _returnToSummaryCommand ?? (_returnToSummaryCommand = new RelayCommand(GoToSummary));
            }
        }

        public void GenerateAssociationRules()
        {
            Messenger.Default.Send(new NotificationMessage<bool>(true, "Wait"));
            _aprioriParameters delApriori = new _aprioriParameters(_app.RunApriori);
            delApriori.BeginInvoke(_confValue, _supportValue, RulesGenerated, null, null);
        }

        public void RulesGenerated(bool complete)
        {
            if (complete)
            {
                Messenger.Default.Send(new NotificationMessage<string>("export", "UpdateMenu"));
                Messenger.Default.Send(new NotificationMessage<bool>(false, "Wait"));
                Messenger.Default.Send(new NotificationMessage<ViewModelBase>(new AssociationRuleResultsViewModel(_app), "ChangeVM"));
            }
        }

        public void GoToSummary()
        {
            Messenger.Default.Send(new NotificationMessage<ViewModelBase>(new DataSummaryViewModel(_app), "ChangeVM"));
        }
    }
}
